<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_departments extends MY_Model {
	
	protected $_table = "departments";
	protected $_uid = "id";

	public function __construct(){
		parent::__construct();
	}

	public function create_default_data()
	{
		$d = 	array('president' => 'President',
							'admin' => 'Administrator',
							'teacher' => 'Teacher',
							'hrd' => 'Human Resource',
							'finance' => 'Finance',
							'evaluation' => 'Evaluation',
							'registrar' => 'Registrar',
							'guidance' => 'Guidance Councilor',
							'librarian' => 'Librarian',
							'custodian' => 'custodian',
							'cashier' => 'Cashier',
							'academichead' => 'Academic Head',
							'student' => 'Student'
						);
		
		foreach ($d as $dep => $desc) {
			unset($data);
			$data['department'] = $dep;
			$data['description'] = $desc;
			$dadta['visible'] = 1;
			if($dep == "student"){
				$data['visible'] = 0;
			}
			$rs = $this->insert($data);
		}
	}

	public function get_dropdown()
	{
		$where = array('visible'=>1);
		$rs = $this->get_for_dd(array('department','description'), $where, false, 'ord');

		if($rs){
			return $rs;
		}else{
			$this->create_default_data();
			return $this->get_for_dd(array('department','description'), $where, false, 'ord');
		}
	}

	/**
	 * Get Departments for Welcome Screen
	 */
	public function get_for_welcome()
	{
		$sql = "
			SELECT 
			id,
			department,
			description,
			icon
			FROM $this->_table
			WHERE visible = 1
			AND department <> 'student'
			ORDER BY `ord`
		";
		return $this->query($sql);
	}

	public function get_all_department_for_welcome_screen($visible = false, $array = false)
	{
		$param = array();
		$filter = " AND department <> 'student'";
		
		if($visible != false){
			$param[] = $visible;
			$filter .= " AND visible = ?";
		}
		
		$sql = "select *
				FROM $this->_table
				WHERE TRUE
				$filter
				ORDER BY level,ord";
		$query = $this->db->query($sql, $param);
		if($array){
			return $query->num_rows() > 0 ? $query->result_array() : FALSE;
		}else{
			return $query->num_rows() > 0 ? $query->result() : FALSE;
		}
	}
	
	public function get_departments_group_by_levels($visible = false, $array = false)
	{
		$param = array();
		$filter = "";
		
		if($visible != false){
			$param[] = $visible;
			$filter = " AND visible = ?";
		}
		
		$sql = "select level
				FROM $this->_table
				WHERE TRUE
				$filter
				GROUP BY level
				ORDER BY level";
		$query = $this->db->query($sql, $param);
		if($array){
			return $query->num_rows() > 0 ? $query->result_array() : FALSE;
		}else{
			return $query->num_rows() > 0 ? $query->result() : FALSE;
		}
	}

	public function get_departments($department)
	{
		$sql = "SELECT * FROM $this->_table WHERE department = ?";
		$query = $this->db->query($sql, array($department));
		return $query->num_rows() > 0 ? $query->row() : FALSE;
	}

	/**
	 * Get Available Department Except those already added in the package
	 * @param int $p_id Package ID
	 * @return array of objects
	 */
	public function get_available_department_except($p_id)
	{
		$sql = "
			SELECT
			id, department, description, visible
			FROM $this->_table d
			WHERE id NOT IN (SELECT department_id from package_departments WHERE package_id = ?)
			ORDER BY department
		";
		return $this->query($sql, array($p_id));
	}

	/**
	 * Get Department that can x students
	 * @param $type table field
	 * @return array of departments
	 */
	public function department_dat_x_student($type)
	{
		$sql = "
			SELECT
			id,
			department
			FROM $this->_table
			WHERE $type = 1
			AND department <> 'student'
		";

		$rs = $this->query($sql);
		$d = array();
		if($rs){
			foreach ($rs as $key => $value) {
				$d[$value->id] = $value->department;
			}
		}

		return $d;

	}
}